<?php

// Include
include_once('include/header.php');
include_once('plugin/ObrasDBPlugin.php');
include_once('plugin/AppConfigPlugin.php');


// Breadcrumb
$breadcrumb = AppConfigPlugin::getBreadcrumb(basename($_SERVER['PHP_SELF']));
$smarty->assign('breadcrumb', $breadcrumb);


// Eliminar
if (isset($_POST[sendform])) {
	if (isset($_POST[action]) && ($_POST[action] == 'removeall')) {
		$message = ObrasDBPlugin::removeObras($_POST[iditems]);
		$count = count(explode(".", $_POST[iditems]));
		if ($count == 1) {
			CommonPlugin::setMessage("La obra se ha eliminado correctamente");
		}
		else if ($count > 1) {
			CommonPlugin::setMessage("Las obras se han eliminado correctamente");
		}
		header( 'Location: obras.php' );
		exit;
	}	
}


// Paginador
$totalItemsPerPage = AppConfigPlugin::getTotalItemsPerPage();
$page = $_GET[page];
if (!$page) {
	$start = 0;
	$page = 1;
}
else
	$start = ($page - 1) * $totalItemsPerPage;


// Ejecucion
$obras = ObrasDBPlugin::getObras($start, $totalItemsPerPage);
$obrasCount = ObrasDBPlugin::getObrasCount();

// Cantidad total de paginas
$totalPages = ceil($obrasCount / $totalItemsPerPage);


// Smarty call
$smarty->assign('obras', $obras);
$smarty->assign('page', $page);
$smarty->assign('totalPages', $totalPages);
$smarty->assign('obrasCount', $obrasCount);

$smarty->display('obras.tpl');

?>